package com.heima.admin.controller.v1;

import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.apis.admin.AdChannelControllerApi;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *
 */
@RestController
@RequestMapping("/api/v1/channel")
public class AdChannelController implements AdChannelControllerApi {

    @Autowired
    AdChannelService adChannelService;

    @Override
    @PostMapping("/list")
    public ResponseResult findByPageAndName(@RequestBody ChannelDto dto) {
        return adChannelService.findByPageAndName(dto);
    }

    @Override()
    @PostMapping("/save")
    public ResponseResult insert(@RequestBody AdChannel channel) {
        return adChannelService.insert(channel);
    }

    @Override
    @PostMapping("/update")
    public ResponseResult update(@RequestBody AdChannel channel) {
        return adChannelService.updateChannel(channel);
    }

    @Override
    @GetMapping("/del/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id) {
        // 测试自主发送异常，测试成功
        /*if (true){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }*/
        return adChannelService.deleteById(id);
    }

    @GetMapping("/channels")
    public ResponseResult findAll(){
        return ResponseResult.okResult(adChannelService.list());
    }

}
